package com.hackordie.operations.queries;

import com.hackordie.commons.HackOrDieResult;
import com.hackordie.commons.authorities.User;
import com.hackordie.exceptions.HackOrDieException;
import com.hackordie.operations.AHackOrDieOperationAuth;

public class QueryUsers extends AHackOrDieOperationAuth {

	private final int id;
	private int first, last;
	private final String username;
	private final boolean allUsers;
	private boolean byUsername;
	private boolean byRange;

	public QueryUsers() {
		super();
		this.id = -1;
		this.username = null;
		this.allUsers = true;
	}

	public QueryUsers(int first, int last) {
		super();
		this.id = -1;
		this.username = null;
		this.allUsers = true;
		this.first = first;
		this.last = last;
		this.byRange = true;
	}

	public QueryUsers(int id) {
		super();
		this.id = id;
		this.username = null;
		this.byUsername = false;
		this.allUsers = false;
	}

	public QueryUsers(String username) {
		super();
		this.id = -1;
		this.username = username;
		this.byUsername = true;
		this.allUsers = false;
	}

	@Override
	protected boolean checkParameters() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected HackOrDieResult executeOperation() throws HackOrDieException {
		if (allUsers) {
			if (byRange) {
				final User[] users = userDao.getByRange(first, last);
				result.setObject(users);
			} else {
				final User[] users = userDao.getAll();
				result.setObject(users);
			}
		} else if (byUsername) {
			final User user = userDao.get(username);
			result.setObject(user);
		} else {
			final User user = userDao.get(id);
			result.setObject(user);
		}
		return result;
	}

}
